﻿Public Class NhanVienDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InUp(ByVal nv As NhanVienDT, ByVal flag As Boolean)
        Dim ma As String = nv.MaNV
        Dim ten As String = nv.TenNV
        Dim dienthoai As String = nv.DienThoai
        Dim diachi As String = nv.DiaChi
        Dim email As String = nv.Email
        Dim id As Integer = nv.ID
        Dim gioitinh As String = nv.GioiTinh
        Dim ngaysinh As Date = nv.NgaySinh
        Dim td As Integer = nv.TrinhDo
        Dim hsl As Integer = nv.HeSoLuong
        Dim tk As String = nv.TaiKhoan
        '----------------------------------------------------'
        If flag = True Then
            sql = "INSERT INTO NhanVien(MaNV,hotenNV,gioitinh,ngaysinh,dienthoai,diachi,email,trinhdo,hesoluong,taikhoan,matkhau) VALUES('" + ma + "',N'" + ten + "',N'" + gioitinh + "','" + ngaysinh + "','" + dienthoai + "',N'" + diachi + "','" + email + "'," & td & "," & hsl & ",'" + tk + "','12345678')"
        Else
            sql = "UPDATE NhanVien SET hotenNV = N'" + ten + "', dienthoai = '" + dienthoai + "', diachi = N'" + diachi + "', email = '" + email + "',gioitinh = N'" + gioitinh + "',ngaysinh = '" + ngaysinh + "',trinhdo = " & td & ",hesoluong = " & hsl & " WHERE MaNV = '" + ma + "' "
        End If
        Return cn.Execute(sql)
    End Function

    Public Function Delete(ByVal nv As NhanVienDT)
        Dim id As Integer = nv.ID
        sql = "delete from NhanVien where Id = " & id
        Return cn.Execute(sql)
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of NhanVienDT)
        Dim id, hsl, td As Integer
        Dim ma, ten, gioitinh, dienthoai, diachi, email As String
        Dim ngaysinh As Date
        Dim nv As NhanVienDT
        Dim list As New List(Of NhanVienDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT MaNV,hotenNV,gioitinh,ngaysinh,dienthoai,diachi,email,trinhdo,hesoluong,Id FROM NhanVien" + dieukien)

        For i As Integer = 0 To dt.Rows.Count() - 1
            id = Convert.ToInt32(dt.Rows(i)("Id"))
            ma = dt.Rows(i)("MaNV").ToString()
            ten = dt.Rows(i)("hotenNV").ToString()
            gioitinh = dt.Rows(i)("gioitinh").ToString()
            ngaysinh = Convert.ToDateTime(dt.Rows(i)("ngaysinh"))
            dienthoai = dt.Rows(i)("dienthoai").ToString()
            diachi = dt.Rows(i)("diachi").ToString()
            email = dt.Rows(i)("email").ToString()
            td = Convert.ToInt32(dt.Rows(i)("trinhdo"))
            hsl = Convert.ToInt32(dt.Rows(i)("hesoluong"))
            '-----'
            nv = New NhanVienDT(ma, ten, dienthoai, diachi, email, gioitinh, ngaysinh, hsl, td, id)
            list.Add(nv)
        Next
        Return list
    End Function

    Function GetID() As Integer
        Dim dt As New DataTable
        Dim id As Integer
        dt = cn.GetData("SELECT * FROM NhanVien")
        If dt.Rows.Count > 0 Then
            id = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1).Item("Id"))
        Else
            id = 1
        End If
        Return id
    End Function
End Class
